﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SisCRA.Business;
using SisCRA.Classes;

namespace SisCRA.Forms
{
    public partial class FormStudentEnrollment : Form
    {
        #region -Constructor-

        /// <summary>
        /// Construtor padrão.
        /// </summary>
        public FormStudentEnrollment()
        {
            try
            {
                InitializeComponent();
                LoadComboBoxSearchDiscipline();
                LoadDataGridViewStudentsEnrollment();
                LoadCheckedListBoxStudents();
                CleanFields();
            }
            catch (Exception ex)
            {
                Log.Instance.Error(ex);
                MessageBox.Show(this, "Falha ao buscar dados.\r\nPor favor, verifique suas configurações de conexão com o banco de dados.", "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #endregion

        #region -Events-

        private void comboBoxSearchDiscipline_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                LoadDataGridViewStudentsEnrollment();
                LoadCheckedListBoxStudents();
                CleanFields();
            }
            catch
            {

            }
        }

        private void buttonCancelEnroll_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.dataGridViewStudentsEnrollment.SelectedRows.Count == 1)
                {
                    StudentEnrollment studentEnrollment = new StudentEnrollment();
                    studentEnrollment.StudentId = int.Parse(this.dataGridViewStudentsEnrollment.SelectedRows[0].Cells[0].Value.ToString());
                    studentEnrollment.DisciplineId = (int)this.comboBoxSearchDiscipline.SelectedValue;

                    StudentEnrollmentBLL studentEnrollmentBLL = new StudentEnrollmentBLL();
                    studentEnrollmentBLL.CancelEnrollStudent(studentEnrollment);

                    LoadDataGridViewStudentsEnrollment();
                    LoadCheckedListBoxStudents();
                    CleanFields();
                }
                else
                {
                    MessageBox.Show(this, "Selecione um aluno para desmatricular!", "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            catch (Exception ex)
            {
                Log.Instance.Error(ex);
                MessageBox.Show(this, "Falha ao cancelar matrícula.", "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void buttonEnroll_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.checkedListBoxStudents.CheckedItems.Count > 0)
                {
                    StudentEnrollment studentEnrollment;
                    StudentEnrollmentBLL studentEnrollmentBLL = new StudentEnrollmentBLL();

                    for (int i = 0; i < checkedListBoxStudents.Items.Count; i++)
                    {
                        if (this.checkedListBoxStudents.GetItemChecked(i))
                        {
                            studentEnrollment = new StudentEnrollment();
                            studentEnrollment.StudentId = (int)((System.Data.DataRowView)(this.checkedListBoxStudents.Items[i])).Row.ItemArray[0];
                            studentEnrollment.DisciplineId = (int)this.comboBoxSearchDiscipline.SelectedValue;

                            studentEnrollmentBLL.EnrollStudent(studentEnrollment);
                        }
                    }

                    LoadDataGridViewStudentsEnrollment();
                    LoadCheckedListBoxStudents();
                    CleanFields();
                }
                else
                {
                    MessageBox.Show(this, "Selecione pelo menos um aluno para matricular!", "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            catch (Exception ex)
            {
                Log.Instance.Error(ex);
                MessageBox.Show(this, "Falha ao matricular aluno(s).", "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        #endregion

        #region -Private Methods-

        /// <summary>
        /// Método que carrega o comboBoxSearchDiscipline.
        /// </summary>
        private void LoadComboBoxSearchDiscipline()
        {
            try
            {
                DisciplineBLL disciplineBLL = new DisciplineBLL();
                this.comboBoxSearchDiscipline.DataSource = disciplineBLL.GetAllDiscipline();
                this.comboBoxSearchDiscipline.ValueMember = "ID";
                this.comboBoxSearchDiscipline.DisplayMember = "CODIGO";
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        /// <summary>
        /// Método que carrega o dataGridViewStudentsEnrollment.
        /// </summary>
        private void LoadDataGridViewStudentsEnrollment()
        {
            try
            {
                Discipline discipline = new Discipline();
                discipline.Id = (int)this.comboBoxSearchDiscipline.SelectedValue;

                StudentEnrollmentBLL studentEnrollmentBLL = new StudentEnrollmentBLL();
                this.dataGridViewStudentsEnrollment.DataSource = studentEnrollmentBLL.GetAllStudentByDiscipline(discipline);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Método que carrega o checkedListBoxStudents.
        /// </summary>
        private void LoadCheckedListBoxStudents()
        {
            try
            {
                Discipline discipline = new Discipline();
                discipline.Id = (int)this.comboBoxSearchDiscipline.SelectedValue;

                StudentEnrollmentBLL studentEnrollmentBLL = new StudentEnrollmentBLL();
                this.checkedListBoxStudents.DataSource = studentEnrollmentBLL.GetStudentNotEnroll(discipline);
                this.checkedListBoxStudents.ValueMember = "ID";
                this.checkedListBoxStudents.DisplayMember = "NOME";
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Método que limpa os campos do formulário.
        /// </summary>
        private void CleanFields()
        {
            this.checkedListBoxStudents.ClearSelected();
        }
        
        #endregion
    }
}
